package com.wise.model.content.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hejiawei
 * @date 2020/10/9 12:51
 * <p>
 * 获取代理商产品类型工具类
 */
public class DealerTypeUtil {

    private static final String KEY = "dealerType";

//    public static Map<Integer, String> getDealerType(String json) {
//        Map<Integer, String> resultMap = new HashMap<>();
//        JSONObject object = JSONObject.parseObject(json);
//        Object type = object.get(KEY);
//        if (type != null) {
//            JSONArray array = JSONArray.parseArray(type.toString());
//            for (int i = 0; i < array.size(); i++) {
//                JSONObject jsonObject = array.getJSONObject(i);
//                resultMap.put(Integer.valueOf(jsonObject.get("val").toString()), String.valueOf(jsonObject.get("name")));
//            }
//        }
//
//        return resultMap;
//
//    }


    public static List<String> getDealerTypeName(String json) {
        List<String> typeNames = new ArrayList<>();
        JSONObject object = JSONObject.parseObject(json);
        Object type = object.get(KEY);
        if (type != null) {
//            JSONObject obj = JSONObject.parseObject(type.toString());
//            for (String key : obj.keySet()) {
//                JSONObject dealerType = obj.getJSONObject(key);
//                typeNames.add(String.valueOf(dealerType.get("name")));
//            }
            JSONArray array = JSONArray.parseArray(type.toString());
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                typeNames.add(String.valueOf(jsonObject.get("name")));
            }
        }

        return typeNames;
    }


    public static Map<String, Integer> getDealerNameMap(String json) {
        Map<String, Integer> resultMap = new HashMap<>();
        JSONObject object = JSONObject.parseObject(json);
        Object type = object.get(KEY);
        if (type != null) {
//            JSONObject obj = JSONObject.parseObject(type.toString());
//            for (String key : obj.keySet()) {
//                JSONObject dealerType = obj.getJSONObject(key);
//                resultMap.put(String.valueOf(dealerType.get("name")), Integer.valueOf(dealerType.get("val").toString()));
//            }
            JSONArray array = JSONArray.parseArray(type.toString());
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                resultMap.put(String.valueOf(jsonObject.get("name")), Integer.valueOf(jsonObject.get("val").toString()));
            }
        }

        return resultMap;
    }


}
